package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.enums.EquipmentEnums;
import com.mxpio.erp.common.equipment.entity.*;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepPlanDetailParamService;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepPlanDetailService;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepPlanService;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepPlanTargetService;
import com.mxpioframework.common.util.SpringUtil;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.SnRuleService;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 点巡检计划Service业务层处理
 *
 * @author Cheng
 * @date 2023-07-19
 */
@Service
public class EquipmentUpkeepPlanServiceImpl extends BaseServiceImpl<EquipmentUpkeepPlan> implements EquipmentUpkeepPlanService {

    @Autowired
    private EquipmentUpkeepPlanTargetService equipmentUpkeepPlanTargetService;

    @Autowired
    private EquipmentUpkeepPlanDetailService equipmentUpkeepPlanDetailService;

    @Autowired
    private EquipmentUpkeepPlanDetailParamService equipmentUpkeepPlanDetailParamService;

    @Override
    @Transactional
    public Result<EquipmentUpkeepPlan> copy(String id) {
        // 主表
        EquipmentUpkeepPlan sourcePlan = this.getById(EquipmentUpkeepPlan.class, id);
        if (sourcePlan == null){
            return Result.error("未找到指定的保养计划");
        }
        EquipmentUpkeepPlan copyPlan = new EquipmentUpkeepPlan();
        BeanUtils.copyProperties(sourcePlan, copyPlan);
        SnRuleService snRuleService = SpringUtil.getBean(SnRuleService.class);
        String expression = "UKP";
        String yearmonth = DateUtils.formatDate(new Date(), "yyyyMM");
        String bizNo = (String) snRuleService.execute(expression+yearmonth+"####", null);
        copyPlan.setCode(bizNo);
        copyPlan.setId(null);
        copyPlan.setLastPlanStart(null);
        copyPlan.setLastPlanEnd(null);
        copyPlan.setCycleIndex(null);
        copyPlan.setIsEnable(Integer.parseInt(CommonEnums.YesNo.NO.getCode()));//默认未开启
        copyPlan.setExecStatus(EquipmentEnums.PlanExecStatus.UNEXEC.getCode());//默认未执行
        this.save(copyPlan);

        // 保养计划明细
        Criteria criteria = Criteria.create();
        criteria.addCriterion("planId", Operator.EQ, sourcePlan.getId());
        List<EquipmentUpkeepPlanDetail> sourceDetailList = equipmentUpkeepPlanDetailService.list(EquipmentUpkeepPlanDetail.class, criteria);
        if (sourceDetailList.size() > 0){
            for (EquipmentUpkeepPlanDetail sourceDetail : sourceDetailList) {
                EquipmentUpkeepPlanDetail copyDetail = new EquipmentUpkeepPlanDetail();
                BeanUtils.copyProperties(sourceDetail, copyDetail);
                copyDetail.setPlanId(copyPlan.getId());
                copyDetail.setId(null);
                equipmentUpkeepPlanDetailService.save(copyDetail);

                // 保养计划明细选项
                Criteria criteria1 = Criteria.create();
                criteria1.addCriterion("detailId", Operator.EQ, sourceDetail.getId());
                List<EquipmentUpkeepPlanDetailParam> list = equipmentUpkeepPlanDetailParamService.list(EquipmentUpkeepPlanDetailParam.class, criteria1);
                if (list.size() > 0){
                    for (EquipmentUpkeepPlanDetailParam sourceDetailParam : list) {
                        EquipmentUpkeepPlanDetailParam copyDetailParam = new EquipmentUpkeepPlanDetailParam();
                        BeanUtils.copyProperties(sourceDetailParam, copyDetailParam);
                        copyDetailParam.setDetailId(copyDetail.getId());
                        copyDetailParam.setId(null);
                        equipmentUpkeepPlanDetailParamService.save(copyDetailParam);
                    }
                }
            }
        }
        List<EquipmentUpkeepPlanTarget> targetList = equipmentUpkeepPlanTargetService.list(EquipmentUpkeepPlanTarget.class, criteria);
        if (targetList.size() > 0){
            for (EquipmentUpkeepPlanTarget sourceTarget : targetList) {
                EquipmentUpkeepPlanTarget copyTarget = new EquipmentUpkeepPlanTarget();
                BeanUtils.copyProperties(sourceTarget, copyTarget);
                copyTarget.setPlanId(copyPlan.getId());
                copyTarget.setId(null);
                copyTarget.setLastPlanStart(null);
                String targetId = copyTarget.getTargetId();
                EquipmentEqpBasics dbEqpBasic = JpaUtil.linq(EquipmentEqpBasics.class).equal("basicsCode", targetId).findOne();
                if (dbEqpBasic!=null&&dbEqpBasic.getLastUpkeepDate()!=null){
                    copyTarget.setLastUpkeepDate(dbEqpBasic.getLastUpkeepDate());
                }else{
                    copyTarget.setLastUpkeepDate(null);
                }
                equipmentUpkeepPlanTargetService.save(copyTarget);
            }
        }
        return Result.OK();
    }
}
